Skip to content

Conversation

@joschmitt
Copy link
Collaborator

@joschmitt joschmitt commented Oct 24, 2025

Add an inplace option to the iterators of exponents, coefficients, terms and monomials of a multivariate polynomial.
This is still a draft. Is this the "interface" we want?
Regarding the new functions coeff!, term! and exponent_vector!: Should they be exported (I did it for now because monomial! is exported as well)? Do I need to implement an "abstract fall-back" in src/MPoly.jl?

Nemocas/Nemo.jl#2161 implements the (few) necessary functions for QQMPolyRingElem. Using those, I get the following timings in oscar-system/Oscar.jl#5483 :

# Previously
julia> @btime inv_c[n-k+1:n];
  3.628 s (28973402 allocations: 1.73 GiB)

# With inplace iterators
julia> @btime inv_c[n-k+1:n];
  1.356 s (2811 allocations: 279.24 KiB)

So that moves in the right direction.

Comment on lines +682 to +686
if !isassigned(m.coeffs, 1) || !is_one(m.coeffs[1])
m.coeffs[1] = one(base_ring(x))
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit ugly, but it gets the monomials iterator to constant allocations. Otherwise it would allocate a new 1 all the time. Is this too much of a corner case?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fine to me

@codecov
Copy link

codecov bot commented Oct 29, 2025

Codecov Report

❌ Patch coverage is 90.90909% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 87.99%. Comparing base (995a497) to head (d62e1c5).
⚠️ Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/MPoly.jl 55.55% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2196      +/-   ##
==========================================
- Coverage   87.99%   87.99%   -0.01%     
==========================================
  Files         127      127              
  Lines       31769    31802      +33     
==========================================
+ Hits        27956    27983      +27     
- Misses       3813     3819       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment on lines 398 to 404
# S may be the type of anything that can store an exponent vector, for example
# Vector{Int}, ZZMatrix, ...
struct MPolyExponentVectors{T <: AbstractAlgebra.RingElem, S}
poly::T
inplace::Bool
temp::S # only used if inplace == true
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exponent_vectors iterator can now in principle take anything as type for the exponents, as long as there is a exponent_vector!(::T, ...) method for that type. Now one can do exponent_vectors(Vector{ZZRingElem}, ::QQMPolyRingElem) and it would for example allow to add a variant exponent_vectors(ZZMatrix, ...) in Nemo like mentioned in oscar-system/Oscar.jl#5483 (comment) .

@joschmitt joschmitt marked this pull request as ready for review October 29, 2025 16:04
@joschmitt joschmitt closed this Oct 30, 2025
@joschmitt joschmitt reopened this Oct 30, 2025
@fingolfin
Copy link
Member

Thank you very much for working on this. It seems fine to me. But I'd like triage to have a look at it before merging.

@joschmitt joschmitt changed the title Towards inplace iterators for exponents of multivariate polynomials etc. "Inplace" iterators over the coefficients, monomials, terms and exponent vectors of a multivariate polynomial Nov 4, 2025
@joschmitt joschmitt added the release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes label Nov 4, 2025
@lgoettgens
Copy link
Member

IMO this should also have the abstract fallbacks, see Nemocas/Nemo.jl#2161 (comment)

@joschmitt
Copy link
Collaborator Author

IMO this should also have the abstract fallbacks, see Nemocas/Nemo.jl#2161 (comment)

Also done.

Copy link
Member

@lgoettgens lgoettgens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some more minor comments. I didn't really think about exponent vectors yet, mostly about the iterators that return some ring elem of some kind

function coefficients(a::MPolyRingElem{T}) where T <: RingElement
return Generic.MPolyCoeffs(a)
function coefficients(a::MPolyRingElem{T}; inplace::Bool = false) where T <: RingElement
return Generic.MPolyCoeffs(a, inplace=inplace)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Generic.MPolyCoeffs(a, inplace=inplace)
return Generic.MPolyCoeffs(a; inplace)

IMO a bit neater, but I don't really care

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I never really got the hang of this syntax, so if you don't care, I'll prefer my version.

Copy link
Member

@lgoettgens lgoettgens left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for working on this!

Comment on lines +518 to +521
If `inplace` is `true`, the elements of the iterator may share their memory. This
means that an element returned by the iterator may be overwritten 'in place' in
the next iteration step. This may result in significantly fewer memory allocations.
Copy link
Collaborator Author

@joschmitt joschmitt Nov 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added these sentences to the doc strings. I'm happy for any suggestions. If I understand the suggestion in oscar-system/Oscar.jl#5530 (comment) correctly, we want to have something to add to all iterator doc strings that allow inplace. So maybe we should agree on a nice wording here and then we can copy and paste it everywhere :)

@fingolfin @mjrodgers

EDIT: I kept this intentionally vague ("may") because with this generic code it might be that the inplace version actually doesn't do anything different (because something is not implemented for a particular type).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this, if others agree I will add this same wording to the other methods in Oscar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mention that it might to lead to unexpected behavior when calling collect on such an iterator.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I concur

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a bit more, so this is ready for the next round of feedback :)

I feel like in an ideal world, we would put this paragraph somewhere "central" in the documentation and just link there from all iterators that support inplace = true. But I don't know where.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks. (And I agree, one "central place" would be great, but for the time being this here is fine.)

@fingolfin fingolfin removed the triage label Nov 12, 2025
@joschmitt
Copy link
Collaborator Author

I'll merge this beginning of next week, if there are no further comments.

@lgoettgens lgoettgens merged commit 0a6e99f into Nemocas:master Nov 17, 2025
23 checks passed
@joschmitt joschmitt deleted the js/inplace branch November 17, 2025 09:40
joschmitt added a commit to joschmitt/AbstractAlgebra.jl that referenced this pull request Nov 20, 2025
Use `@inferred` correctly and fix return type
joschmitt added a commit to joschmitt/AbstractAlgebra.jl that referenced this pull request Nov 24, 2025
Use `@inferred` correctly and fix return type
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release notes: use title For PRs: the title of this PR is suitable for direct use in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants